# -*- coding: utf-8 -*-
import sys
sys.path.append("../../quidditas")

from random import randint

import pygame
import quidditas

from components import *

class ChimpMovementProcessor(quidditas.Processor):
    """ Update entities that are moving through the world and
    make sure they don't leave the world. """
    component_types = (SpriteComponent, ChimpComponent)

    def __init__(self):
        super(ChimpMovementProcessor, self).__init__()
        self.area = pygame.display.get_surface().get_rect()

    def update_entity(self, time, entity, components):
        chimp = components[ChimpComponent]
        sprite = components[SpriteComponent].sprite
        if chimp.dizzy:
            self._spin(chimp, sprite)
        else:
            self._walk(chimp, sprite)

    def _walk(self, chimp, sprite):
        rect = sprite.rect
        newpos = rect.move((chimp.move, 0))
        if not self.area.contains(newpos):
            if rect.left < self.area.left or rect.right > self.area.right:
                chimp.move = -chimp.move
                newpos = rect.move((chimp.move, 0))
                sprite.image = pygame.transform.flip(sprite.image,1, 0)
        sprite.rect = newpos

    def _spin(self, chimp, sprite):
        center = sprite.rect.center
        chimp.dizzy += 12
        if chimp.dizzy >= 360:
            chimp.dizzy = 0
            sprite.image = chimp.original
        else:
            rotate = pygame.transform.rotate
            sprite.image = rotate(chimp.original, chimp.dizzy)
        sprite.rect = sprite.image.get_rect(center=center)

class FistControlProcessor(quidditas.Processor):
    """ Moves the fist to the current position of the mouse cursor. """
    component_types = (SpriteComponent, FistComponent)

    def update_entity(self, time, entity, components):
        mouse_pos = pygame.mouse.get_pos()
        rect_component = components[SpriteComponent]
        fist = components[FistComponent]
        rect_component.sprite.rect.midtop = mouse_pos
        if fist.is_punching:
            rect_component.sprite.rect.move_ip(5, 10)

class PunchProcessor(quidditas.Processor):
    component_types = ()

    def __init__(self, sounds):
        self.sounds = sounds
        super(PunchProcessor, self).__init__()

    def update(self, time):
        for event in pygame.event.get([pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP]):
            chimp = self.quidditas.get_entities_with_component(ChimpComponent)[0]
            chimp_component = chimp.get_component(ChimpComponent)
            chimp_sprite = chimp.get_component(SpriteComponent)

            fist = self.quidditas.get_entities_with_component(FistComponent)[0]
            fist_component = fist.get_component(FistComponent)
            fist_sprite = fist.get_component(SpriteComponent)

            if event.type == pygame.MOUSEBUTTONDOWN:
                if not fist_component.is_punching:
                    fist_component.is_punching = True
                    if fist_sprite.sprite.rect.colliderect(chimp_sprite.sprite.rect):
                        chimp_component.dizzy = True
                        chimp_component.original = chimp_sprite.sprite.image
                        self.sounds["punch"].play()
                    else:
                        self.sounds["whiff"].play()
            elif event.type == pygame.MOUSEBUTTONUP:
                fist_component.is_punching = False

class DrawTextProcessor(quidditas.Processor):
    component_types = ()

    def __init__(self, screen):
        super(DrawTextProcessor, self).__init__()
        self.screen = screen

    def update(self, time):
        if pygame.font:
            font = pygame.font.Font(None, 36)
            text = font.render("Pummel The Chimp, And Win $$$", 1,(10, 10, 10))
            textpos = text.get_rect(centerx=self.screen.get_size()[0]/2)
            self.screen.blit(text, textpos)
        else:
            print "no font :("

class RenderProcessor(quidditas.Processor):
    """ Renders all visible entities. """
    component_types = (SpriteComponent)
    
    def __init__(self, screen):
        """ Create a new renderprocessor that renders to screen and
        uses images as the image source. """
        super(RenderProcessor, self).__init__()
        self.screen = screen
        self.super_update = super(RenderProcessor, self).update

    def update(self, time):
        """ This method is overridden to clear the screen on each draw-update before drawing the components. """
        self.screen.fill((250,250,250))
        self.super_update(time)

    def update_entity(self, time, entity, components):
        """ Renders all renderable-entities at their positions. """
        sprite_component = components[SpriteComponent]
        self.screen.blit(sprite_component.sprite.image, sprite_component.sprite.rect)
